<?php

/**
 * ctl_certificate，证书验证控制器类
 * 继承adminPage
 */
class ctl_certificate extends adminPage
{
    var $workground = 'setting';

    function ctl_certificate()
    {
        parent::adminPage();
        $this->lang = 'zh-cn';//中文
        $this->base_url = 'http://service.shopex.cn/info.php';
        $this->sess_id = $this->system->session->sess_id;//会话id，这是一个MD5串，保存在数据库sdb_op_sessions表里
        $this->license_url = "index.php?ctl=service/certificate&act=download";//授权证书下载地址
    }

    /**
     * showIndex，显示授权证书页面
     *
     */
    function showIndex()
    {
        if (constant('SAAS_MODE')) {
            exit;
        }
        $this->path[] = array('text' => __('ShopEx证书'));
        $this->certi_model = &$this->system->loadModel('service/certificate');
        $this->Certi = $this->certi_model->getCerti();//验证编号
        $this->Token = $this->certi_model->getToken();//标记
        if (empty($this->Certi) || empty($this->Token)) {
            $this->pagedata['license'] = true;
        } else {
            $this->pagedata['license'] = true;
            $this->pagedata['license_url'] = $this->license_url;
        }
        $this->pagedata['certi_id'] = $this->Certi;
        $this->pagedata['debug'] = false;
        $this->pagedata['certtype'] = $this->system->getConf('system.certtype');
        $this->page('service/index.html');
    }

    /**
     * upLicense，重置证书
     *
     */
    function upLicense()
    {
        $this->certi_model = &$this->system->loadModel('service/certificate');
        $result1 = $this->certi_model->checkFile($_FILES['license']['tmp_name']);
        if (!$result1) {
            $this->splash('failed', 'index.php?ctl=service/certificate&act=showIndex', __('重置证书失败，请先上传文件'));
            exit;
        }
        $result = $this->certi_model->upload($_FILES['license']['tmp_name']);
        if (!$result) {
            $this->splash('failed', 'index.php?ctl=service/certificate&act=showIndex', __('证书重置失败,请先上传文件'));
            exit;
        } else {
            $this->splash('success', 'index.php?ctl=service/certificate&act=showIndex', __('证书重置成功'));
            exit;
        }
    }

    /**
     * inputto，功能未知
     *
     */
    function inputto()
    {
        $this->certi_model = &$this->system->loadModel('service/certificate');
        $this->certi_model->inputto();
    }

    /**
     * download，证书下载
     * 文件名为：网店名称+CERTIFICATE.CER
     * 内容为为：证书id+|||+2个md5值（64个字符），Certid|||Token
     */
    function download()
    {
        header("Content-type:application/octet-stream;charset=utf-8");
        header("Content-Type: application/force-download");
        $this->certi_model = &$this->system->loadModel('service/certificate');
        $charset = &$this->system->loadModel('utility/charset');
        $this->fileName = $charset->utf2local($this->certi_model->getName() . 'CERTIFICATE.CER', 'zh');
        header("Content-Disposition:filename=" . $this->fileName);
        $this->Certi = $this->certi_model->getCerti();
        $this->Token = $this->certi_model->getToken();
        echo $this->Certi;
        echo '|||';
        echo $this->Token;
    }

    /**
     * del，清空证书
     * 要求管理员密码验证
     */
    function del()
    {
        $this->certi_model = &$this->system->loadModel('service/certificate');
        $this->certi_model->checkPass($_POST);
        if (!$this->certi_model->checkPass($_POST)) {
            $this->splash('failed', 'index.php?ctl=service/certificate&act=checkPass', __('请输入正确的用户名和密码'));
            exit;
        }
        $this->certi_model->delLicense();
        $this->splash('success', 'index.php?ctl=service/certificate&act=showIndex', __('证书清空成功'));
    }

    /**
     * checkPass，密码验证
     */
    function checkPass()
    {
        $this->page('service/checkp.html');
    }

}

?>
